<?php
/**
 * Login_Model_Logged
 * check user data
 * 
 * @author Adrian Stolarski
 */
class Login_Model_Logged implements Login_Model_Mainable, Login_Model_Checkable, 
        Login_Model_Getable, Login_Model_Setable, Login_Model_Redirectable {
    
    /**
     *
     * @var type $check_data
     * check data handler
     */
    public $check_data;
    /**
     *
     * @var type $get_data
     * get user data handler
     */
    public $get_data;
    /**
     *
     * @var type $redirect_action
     * redirect action handler
     */
    public $redirect_action;
    
    /**
     * session data handler
     * @var type $isset_session_data
     */
    public $isset_session_data;
    /**
     *
     * @var type $isset_session_redirect
     * session initlialize and redirect
     */
    public $isset_session_redirect;
    /**
     * main()
     * logged implementation
     * 
     * @param array $variables
     */
    public function main(array $variables = null) {
        //var_dump($variables);
        $user_id = $this->check_data($variables['post']);
        $user_data = array();
        $user_data = $this->get_user_data($user_id);
        $user_data['csrf_token'] = $variables['post']['csrf_token'];
        //var_dump($user_data);
        $this->set_data($user_data);
        $this->redirect_action($_SESSION);
    }
    
    /**
     * check_data()
     * check user data and return user id
     * @param array $variables
     * @return type $id
     */
    public function check_data(array $variables = null) {
        //var_dump($variables);
        
        $this->check_data = new Login_Data_Loggeddatacheckout;
        $id = $this->check_data->main($variables);
        if(isset($id) && (!is_null($id)) && (trim($id) != '')){
            //print_r($id);
            return $id;
        }/**
        else {
            $this->redirect_action = new Login_Data_Loginredirect;
            $this->redirect_action->main($variables);
        }**/
    }
    
    /**
     * get_user_data()
     * get user data
     * 
     * @param type $value
     * @return array $user_data
     */
    public function get_user_data($value, $key = null) {
        $variables = array();
        $variables['user_id'] = $value;
        //var_dump($variables);
        $this->get_data = new Login_Data_Getloggeddata;
        $user_data = $this->get_data->main($variables);
        //var_dump($user_data);
        return $user_data;
    }
    
    /**
     * set_data()
     * set session data
     * @param array $variables
     */
    public function set_data(array $variables = null) {
        //var_dump($variables);
        if(isset($variables['csrf_token']) && ($_SESSION['csrf_token'] == $variables['csrf_token'])){
            $this->isset_session_data = new Login_Data_Usersession;
            $this->isset_session_data->main($variables);
        }
        else {
            $this->redirect_action($variables);
        }
    }
    
    /**
     * redirect_action()
     * user is logged redirect
     * @param array $variables
     */
    public function redirect_action(array $variables = null) {
        if($_SESSION['user_access_level_id'] != 0){
            $this->redirect_action = new Login_Data_Loggedredirect;
            $this->redirect_action->main($variables);
        }
        else {
            $this->redirect_action = new Login_Data_Loginredirect;
            $this->redirect_action->main($variables);
        }
    }

}
